
package com.greenantmall.view;

import com.greenantmall.R;
import com.greenantmall.support.ViewCompat;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CommonLoadingAndErrorDialog extends Dialog {
    private Activity activity;
    private ProgressBar loadingPb;
    private ImageView errorIv;
    private TextView tipTv;
    private boolean isErrorMode;

    public CommonLoadingAndErrorDialog(Context context) {
        this(context, R.style.exit_dialog_style);
    }

    public CommonLoadingAndErrorDialog(final Context context, int theme) {
        super(context, theme);
        activity = (Activity)context;
        isErrorMode = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_loading_and_error_dialog_view);
        //getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        loadingPb = (ProgressBar) findViewById(R.id.pb_loading);
        errorIv = (ImageView) findViewById(R.id.iv_error);
        tipTv = (TextView) findViewById(R.id.tv_loading_tip);
        ViewCompat.animate(loadingPb)
                .alpha(1)
                .setDuration(0)
                .start();
        ViewCompat.animate(errorIv)
                .alpha(0)
                .setDuration(0)
                .start();
        ViewCompat.animate(tipTv)
                .alpha(0)
                .setDuration(0)
                .start();
        isErrorMode = false;
    }

    public void setLoadError(String errorTip) {
        tipTv.setText(errorTip);
        isErrorMode = true;
        ViewCompat.animate(loadingPb)
                .alpha(0)
                .setDuration(150)
                .start();
        ViewCompat.animate(errorIv)
                .alpha(1)
                .setDuration(150)
                .start();
        ViewCompat.animate(tipTv)
                .alpha(1)
                .setDuration(150)
                .start();
    }
    
    @Override
    public void onBackPressed() {
        if(isShowing() && !isErrorMode) {
            dismiss();
        } else {
            super.onBackPressed();
        }
    }

}
